﻿Imports System.Web.UI.WebControls

Namespace DotNetNuke.Modules.ActiveForums
    Partial Public Class af_quickjump
        Inherits ForumBase
        Public MOID As Integer
        Private _dtForums As DataTable = Nothing
        Protected WithEvents drpForums As New DropDownList
        Public Property dtForums() As DataTable
            Get
                Return _dtForums
            End Get
            Set(ByVal value As DataTable)
                _dtForums = value
            End Set
        End Property
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try
                If drpForums Is Nothing Then
                    drpForums = New DropDownList
                End If
                drpForums.AutoPostBack = True
                drpForums.CssClass = "afdropdown"
                BindForums()
                Controls.Add(drpForums)
            Catch exc As Exception
                DotNetNuke.Services.Exceptions.ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
        Private Sub BindForums()
            Dim fc As New ForumController()
            dtForums = fc.GetForumView(PortalId, MOID, UserId, UserInfo.IsSuperUser, UserForumsList)
         
            Dim dr As DataRow = Nothing
            drpForums.Items.Clear()
            drpForums.Items.Insert(0, New ListItem(String.Empty, String.Empty))
            Dim i As Integer = 0
            Dim n As Integer = 1
            Dim tmpGroupCount As Integer = 0
            Dim tmpForumCount As Integer = 0
            Dim tmpGroupKey As String = String.Empty
            Dim tmpForumKey As String = String.Empty
            For Each dr In dtForums.Rows
                Dim bView As Boolean = Permissions.HasPerm(dr("CanView").ToString, ForumUser.UserRoles)
                Dim GroupName As String = CStr(dr("GroupName"))
                Dim GroupId As Integer = CInt(dr("ForumGroupId"))
                Dim GroupKey As String = GroupName & GroupId.ToString
                Dim ForumName As String = CStr(dr("ForumName"))
                Dim ForumId As Integer = CInt(dr("ForumId"))
                Dim ForumKey As String = ForumName & ForumId.ToString
                Dim ParentForumId As Integer = CInt(dr("ParentForumId"))
                'TODO - Need to add support for Group Permissions and GroupHidden

                If tmpGroupKey <> GroupKey Then
                    drpForums.Items.Insert(n, New ListItem(GroupName, "GROUPJUMP:" & GroupId))
                    n += 1
                    tmpGroupKey = GroupKey
                End If
                If bView Then
                    If ParentForumId = 0 Then
                        drpForums.Items.Insert(n, New ListItem("--" & dr("ForumName").ToString, "FORUMJUMP:" & dr("ForumID").ToString))
                        n += 1
                        n = GetSubForums(n, CInt(dr("ForumId")))
                    End If

                End If






            Next

            If Not GetViewType() Is Nothing Then
                Dim sView As String = GetViewType()
                If sView = "TOPICS" Or sView = "TOPIC" Then
                    Dim sForum As String = "FORUMJUMP:" & ForumId
                    drpForums.SelectedIndex = drpForums.Items.IndexOf(drpForums.Items.FindByValue(sForum))
                End If
            End If
        End Sub
        Private Function GetSubForums(ByVal ItemCount As Integer, ByVal ParentForumId As Integer) As Integer
            Dim dr As DataRow = Nothing
            dtForums.DefaultView.RowFilter = "ParentForumId = " & ParentForumId
            If dtForums.DefaultView.Count > 0 Then
                For Each dr In dtForums.DefaultView.ToTable.Rows
                    If Permissions.HasPerm(dr("CanView").ToString, ForumUser.UserRoles) Then
                        drpForums.Items.Insert(ItemCount, New ListItem("----" & dr("ForumName").ToString, "FORUMJUMP:" & dr("ForumID").ToString))
                        ItemCount += 1
                    End If

                Next

            End If
            Return ItemCount
        End Function
        Private Sub drpForums_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles drpForums.SelectedIndexChanged
            ' Try
            Dim sJumpValue As String = drpForums.SelectedItem.Value
            If Not sJumpValue = String.Empty And Not sJumpValue = "" Then
                Dim sJumpType As String = Left(sJumpValue, InStr(sJumpValue, ":") - 1)
                Dim sJumpID As String = Mid(sJumpValue, InStr(sJumpValue, ":") + 1)
                Select Case sJumpType
                    Case "GROUPJUMP"
                        Response.Redirect(NavigateUrl(TabId, "", ParamKeys.GroupId & "=" & sJumpID))
                    Case "FORUMJUMP"
                        Dim Params() As String = {ParamKeys.ViewType & "=" & Views.Topics, ParamKeys.ForumId & "=" & sJumpID}
                        Response.Redirect(NavigateUrl(TabId, "", Params))
                End Select
            End If

            'Catch ex As Exception

            'End Try

        End Sub
    End Class
End Namespace
